/**
 * Copyright(c)2012 Beijing PeaceMap Co.,Ltd.
 * All right reserved. 
 */
package com.pmc.dwa.common.dialect;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.ClassUtils;

/**
 * @description 调用方言工具类
 * @author aokunsang
 * @date 2013-1-6
 */
public final class DialectUtil {
	public static Log log = LogFactory.getLog(DialectUtil.class);
	
	final static String DEFAULT_DIALECT_PATH = "dialect.properties";
	final static String DEFAULT_KEY = "dialect";
	private static Properties defaultStrategies;
	
	/**
	 * 获取有效的数据库方言
	 * 注意：项目使用哪种方言，需要在dialect.properties中配置；并且实现Dialect接口
	 * @return
	 */
	 public static Dialect getDialectInstance(){
		try {
			ClassPathResource resource = new ClassPathResource(DEFAULT_DIALECT_PATH, DialectUtil.class);
			defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
			String value = defaultStrategies.getProperty(DEFAULT_KEY);
			Class<?> clazz = ClassUtils.forName(value, DialectUtil.class.getClassLoader());
			return (Dialect)clazz.newInstance();
		} catch (Exception e) {
			log.error("-----没有找到有效的方言，使用默认SqlServer方言----", e);
			return new SqlServerDialect();
		}
	}
}
